<?php
function fogbugz_admin_form(&$form_state) {
  $form = array();
  //check if the lib exist
  if (!is_file(drupal_get_path('module', 'fogbugz'). '/FogbugzRestLib.php')) {
    form_set_error(NULL, t('The file "FogbugzRestLib.php" does not exist'));
    return;
  }
  //load settings
  $settings = variable_get('fogbugz_settings', array());
  $form['basic'] = array(
    '#type' => 'fieldset',
    '#title' => t('Basic Info'),
  );
  $form['basic']['url'] = array(
    '#type' => 'textfield',
    '#title' => t('Fogbugz Url'),
    '#default_value' => !empty($settings['url'])?$settings['url']:'',
    '#required' => TRUE,
  );
  $form['basic']['email'] = array(
    '#type' => 'textfield',
    '#title' => t('Fogbugz User Email'),
    '#default_value' => !empty($settings['email'])?$settings['email']:'',
    '#required' => TRUE,
  );
  $form['basic']['password'] = array(
    '#type' => 'password',
    '#title' => t('Password'),
    '#description' => t('This value will not be stored. The only use is to generate token'),
  );
  $form['basic']['token'] = array(
    '#type' => 'textfield',
    '#title' => t('Token'),
    '#default_value' => !empty($settings['token'])?$settings['token']:'',
  );
  $form['sync_taxonomy'] = array(
    '#type' => 'fieldset',
    '#title' => t('Sync Projects'),
    '#description' => t('Fogbugz module will pull the projects\'name from Fogbugz and save as terms'),
  );
  $form['sync_taxonomy']['sync_enable'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable'),
    '#default_value' => !empty($settings['sync_enable'])?$settings['sync_enable']:0,
  );
  $vocs = array();
  $vocabularies = taxonomy_get_vocabularies();
  foreach ($vocabularies as $v) {
    $vocs[$v->vid] = $v->name;
  }
  $form['sync_taxonomy']['vocabulary'] = array(
    '#type' => 'select',
    '#title' => t('Default Project Vocabulary'),
    '#options' => $vocs,
    '#default_value' => !empty($settings['vocabulary'])?$settings['vocabulary']:NULL,
  );
  $form['save'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  return $form;
}

function fogbugz_admin_form_validate(&$form, &$form_state) {
  $file_path = drupal_get_path('module', 'fogbugz'). '/FogbugzRestLib.php';
  if (!is_file($file_path)) {
    form_set_error(NULL, t('File "FogbugzRestLib.php" does not exist'));
  }
}

function fogbugz_admin_form_submit($form, &$form_state) {
  require_once(drupal_get_path('module', 'fogbugz'). '/FogbugzRestLib.php');
  try {
    if (!empty($form_state['values']['token'])) {
      $client = new FogbugzRestLib($form_state['values']['url'], $form_state['values']['token'], $form_state['values']['email'], $form_state['values']['password']);
    }
    else {
      $client = new FogbugzRestLib($form_state['values']['url'], NULL, $form_state['values']['email'], $form_state['values']['password']);
    }
    $form_state['values']['token'] = (string) $client->getToken();
    $settings = array(
      'url' => $form_state['values']['url'],
      'email' => $form_state['values']['email'],
      'token' => $form_state['values']['token'],
      'vocabulary' => $form_state['values']['vocabulary'],
      'sync_enable' => $form_state['values']['sync_enable'],
    );
    variable_set('fogbugz_settings', $settings);
  }
  catch (Exception $e) {
    drupal_set_message('Fogbugz Errors: '. $e->getMessage(), 'error');
  }
}